package com.jbit.bolon.redis;

import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnection;
import redis.clients.jedis.Jedis;

public class RedisTemplate extends org.springframework.data.redis.core.RedisTemplate {

    public static ThreadLocal<Integer> indexdb = new ThreadLocal<Integer>(){
        @Override protected Integer initialValue(){ return 0;}
    };

    protected RedisConnection preProcessConnection (RedisConnection connection,boolean existingConnection){
        try {
            Integer dbIndex = indexdb.get();
            if (dbIndex != null){
                if (connection instanceof JedisConnection){
                    if (((JedisConnection) connection).getNativeConnection().getDB() != dbIndex){
                        connection.select(dbIndex);
                    }
                }else {
                    connection.select(dbIndex);
                }
            } else {
                connection.select(0);
            }
        } finally {
            indexdb.remove();
        }
        return super.preProcessConnection(connection, existingConnection);
    }

}
